package com.zjy.org.service.impl;

import com.zjy.basic.query.BaseQuery;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.basic.util.PageList;
import com.zjy.basic.constant.Constant;
import com.zjy.org.domain.Department;
import com.zjy.org.mapper.DepartmentMapper;
import com.zjy.org.query.DepartmentQuery;
import com.zjy.org.service.DepartmentService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        this.update(department);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" )
    public void update(Department department) {
        String dirPath = "/";
        //如果父级对象等于null，自己属于顶级部门
        if(department.getParent() == null){
            dirPath = dirPath + department.getId();
        }else{
            //查询父部门
            Department parent = departmentMapper.findById(department.getParent().getId());
            dirPath = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" )
    public void del(Long id) {
        departmentMapper.del(id);
    }

    @Override
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" )
    public void patchDel(Long[] ids) {
        departmentMapper.patchDel(ids);
    }

    @Override
    public PageList<Department> queryPage(BaseQuery baseQuery) {
        //1.查询总数量
        Integer totals = departmentMapper.queryCount(baseQuery);
        if (totals==null){
            return new PageList<>();
        }
        //2.查询当前页数据
        List<Department> rows = departmentMapper.queryData(baseQuery);
        return new PageList<>(totals,rows);
    }

    @Override
    @Cacheable(cacheNames = Constant.DEPT_TREE_KEY,key = "'tree'") //注意！加了这个注解必须开启redis才能正常使用！！！
    public List<Department> deptTree() {
        return getDepartmentsTree();
    }

    private List<Department> getDepartmentsTree() {
        //获取到所有的部门
        List<Department> departments = departmentMapper.findAll();
        //先，生成一个map
        HashMap<Long, Department> map = new HashMap<>();
        //把所有的部门都装进map中，key为部门id，value为部门对象本身
        for (Department department : departments) {
            map.put(department.getId(), department);
        }
        //新建一个部门树，用于返回
        ArrayList<Department> deptTree = new ArrayList<>();
        //再次遍历所有部门
        for (Department department : departments) {
            if (department.getParent_id() == null) {//判断为顶级部门
                deptTree.add(department);//顶级部门就直接添加进去
            } else {//否则就为次级部门
                Long parent_id = department.getParent_id();//次级部门就需要获取他的父部门id
                Department parentDepartment = map.get(parent_id);
                if (parentDepartment != null) {
                    parentDepartment.getChildren().add(department);
                }
            }
        }
        return deptTree;
    }
}
